<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	/*******main*******/
	$this->model('member')->checklogin();
	if(intval($_RQ['fromuser'])){
		$this->model('member')->setfromuser();
	}
	$id = intval($_RQ['id']);
	if(!$id){
		exi('该仓不存在!','error');
	}
	$this->session('cangid',$id);
	$team=pdo_get('jy_jicai_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($team && $team['status']==0){
		$this->model('model')->add_yao($team);
	}
	$team['isnum']=pdo_count('jy_jicai_yao',array('cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	$team['isfee']=pdo_getsum('jy_jicai_canlog',array('cangid'=>$team['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
	$return['members']=$members;
	$team['statustitle']='快邀请好友一起来参与倍财吧！';
	$return['team']=$team;
	$return['list']=array();
	$return['post']=array();
	$return['params']=array('status'=>0,'cangid'=>$team['id']);
	$return['xieyi']=$this->plugin_setting('beicaixieyi');
	$return['pluginset']['jian_feetype']=$return['pluginset']['jian_feetype']?:3;
	$return['pluginset']['jian_feetypetitle']=$this->credit_type[$return['pluginset']['jian_feetype']];
	$return['sharedata']=array(
			'title'=>$nickname.'-发起倍财金收益啦！满'.$team['mannum'].'人就可获利了！',
			'desc'=>'参与倍财金收益啦，满'.$team['mannum'].'人就可获利了！',
			'link'=>custom_url('jy_jicai/licai.detail//id='.$team['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($team['image'])
	);
	set_html('title','建仓详情');
	exi($return);	
}
if($_RQ['op']=='getlist'){
	$team=pdo_get('jy_jicai_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('cangid')));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'cangid'=>$this->session('cangid'));
	if($_RQ['status']==3){
		$params['status']=array(3,4,6,7);
	}elseif($_RQ['status']==1){
		$params['status']=array(2,5);
	}else{
		$params['status']=array(0,1);
	}
    $orderby='paixu asc';
	$total='';
	$return['list']=pdo_getslice('jy_jicai_yao',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	$jian_days=$this->setting['jian_days']?:3;
	$this->setting['jian_feerate']=$this->setting['jian_feerate']?:30;
	foreach($return['list'] as &$v){
			$this->model('model')->up_yao($v);
			if($v['status']==1 && $v['endtime']<=TIMESTAMP){
				$v['status']=3;
			}
			if($v['status']==0 && $v['endtime']<=TIMESTAMP){
				$v['status']=7;
			}
			if($v['status'] <=1 && $team['status']==2){
				pdo_update('jy_jicai_yao',array('status'=>7),array('id'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
				$v['status']=7;
			}
			if(!$v['status'] && $v['startime']<=TIMESTAMP){
				$v['status']=$this->model('model')->start_yao($v);
			}
			$v['title']='第'.$team['paixu'].'仓第'.$v['paixu'].''.$this->y_title;
			$v['endtime']=date('Y-m-d',$v['endtime']);
			$v['startime']=date('Y-m-d',$v['startime']);
			$v['statustitle']=$this->yao_status[$v['status']];
			$v['isfee']=pdo_getsum('jy_jicai_canlog',array('yaoid'=>$v['id'],'cangid'=>$this->session('cangid'),'uniacid'=>$_SESSION['uniacid']),'jifen');
			$v['israte']=$v['isfee']?price($v['isfee']*100/$v['alljifee']):0;
			$v['jian_getrate']=$team['jian_getrate'];
			$v['jian_jinmin']=$team['jian_jinmin'];
			$v['jian_jinmax']=$v['jian_jinmax']?:$team['jian_jinmax'];
            $v['jian_feetype']=$v['jian_jinmax'];
			if($this->setting['jian_feerate']){
                $v['jian_feetype']=$v['jian_jinmax']*$this->setting['jian_feerate']/100;
            }
            if($this->setting['jian_onemaxfeenum']){
                $v['jian_feetype']=$this->setting['jian_onemaxfeenum'];
            }
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$yaoid=intval($post['yaoid']);
	$cangid=$this->session('cangid');
	$post['jifen']=intval($post['jifen']);
	if(!$post['jifen']){
		exi('请输入要投入积分额度,积分必须大于等于1！','error');
	}
	$team=pdo_get('jy_jicai_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cangid));
	$yao=pdo_get('jy_jicai_yao',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yaoid));
	$jian_jinmax=$yao['jian_jinmax'];
	if($post['jian_feetype']==1){
        if($this->setting['jian_feerate']){
            $jian_jinmax=$v['jian_jinmax']*$this->setting['jian_feerate']/100;
        }
        if($this->setting['jian_onemaxfeenum']){
            $jian_jinmax=$this->setting['jian_onemaxfeenum'];
        }
	}
	$member=pdo_get('jy_jicai_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($member['is_lastmiao']){
		$jian_lastmiao=intval($this->setting['jian_lastmiao']);
		$istime=TIMESTAMP-$yao['startime'];
		if($jian_lastmiao>0 && $jian_lastmiao>$istime){
			$lastmiao=$jian_lastmiao-$istime;
		}
		if($lastmiao>0){
			$lastmiao=$lastmiao>60?60:$lastmiao;
			sleep($lastmiao);
		}
	}
	$this->model('model')->check_buy($yao,$post['jifen'],$post['jian_feetype']);
	if($post['jifen']>$jian_jinmax && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入额度不能大于'.$jian_jinmax.'！','error');
	}
	if($post['jifen']<$team['jian_jinmin'] && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入积分额度不能小于'.$team['jian_jinmin'].'！','error');
	}
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$btype=1;
	if($post['jian_feetype']){
		$btype=$this->setting['jian_feetype']?:6;
	}
	if($member['credit'.$btype]<$post['jifen']){
		exi('您的积分不足，不能参与！','error');
	}
	$log=array(
			'cangid'=>$cangid,
			'yaoid'=>$yaoid,
			'jifen'=>$post['jifen'],
	);
	$this->model('model')->up_canlog($log,$post['jian_feetype']);
	exi('参与购买成功！','','jy_jicai/canlog/');
}
?>